package com.zbkj.service.service.mszs;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zbkj.common.model.mszs.MszsHotel;
import com.zbkj.common.model.mszs.MszsRoom;
import com.zbkj.common.model.mszs.MszsRoomType;
import com.zbkj.common.request.mszs.MszsCenterRequest;
import com.zbkj.common.request.mszs.MszsRoomRequest;
import com.zbkj.common.request.mszs.MszsRoomSelectRequest;
import com.zbkj.common.response.mszs.MszsHotelResponse;
import com.zbkj.common.response.mszs.MszsRoomOrderResponse;
import com.zbkj.common.response.mszs.MszsRoomResponse;
import com.zbkj.common.token.FrontTokenComponent;
import com.zbkj.common.utils.DateUtil;
import com.zbkj.service.dao.mszs.MszsRoomDao;
import com.zbkj.service.service.publics.attachment.SystemAttachmentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @description 民宿助手酒店房间信息
 * @author zhengkai.blog.csdn.net
 * @date 2023-11-16
 */
@Service
public class MszsRoomServiceImpl extends ServiceImpl<MszsRoomDao, MszsRoom> implements MszsRoomService {

    @Autowired
    private MszsRoomDao roomDao;
    @Autowired
    private MszsRoomTypeService roomTypeService;
    @Autowired
    private MszsHotelService hotelService;
    @Autowired
    private FrontTokenComponent tokenComponent;
    @Autowired
    private MszsOrderService orderService;
    @Autowired
    private SystemAttachmentService systemAttachmentService;
    @Override
    public boolean insert(MszsRoomRequest request) {
       if (StringUtils.isNotEmpty(request.getFjNumber())){
            MszsRoom room = roomNumber(request.getFjNumber());
            if (Objects.nonNull(room)){
                return false;
            }
        }
        MszsRoom room=new MszsRoom();
        BeanUtils.copyProperties(request, room);
        if (request.getFjImg()!=null && request.getFjImg().size()>0){ //判断是否有图片
            //list转string
            String img = StringUtils.join(request.getFjImg(), ",");
            room.setFjImg(systemAttachmentService.clearPrefix(img));
        }

        int ro = roomDao.insert(room);
        return ro > 0;
    }

    @Override
    public boolean update(MszsRoomRequest request) {
        if (StringUtils.isNotEmpty(request.getFjNumber())){
            MszsRoom room = roomNumber(request.getFjNumber());
            if (Objects.nonNull(room)){
                return false;
            }
        }
        MszsRoom mszsRoom=new MszsRoom();
        BeanUtils.copyProperties(request, mszsRoom);
        if (request.getFjImg()!=null && request.getFjImg().size()>0){  //判断是否有图片
            //list转string
            String img = StringUtils.join(request.getFjImg(), ",");
            mszsRoom.setFjImg(systemAttachmentService.clearPrefix(img));
        }
        int ro = roomDao.updateById(mszsRoom);
        return ro > 0;
    }

    @Override
    public boolean upRoomStatus(List<MszsRoom> requests) {
        boolean bool = this.updateBatchById(requests);
        return bool;
    }

    @Override
    public boolean delete(Integer id) {
        int ro = roomDao.deleteById(id);
        return ro > 0;
    }

    @Override
    public MszsRoomResponse load(Integer id) {
        MszsRoom room = roomDao.selectById(id);

        MszsRoomResponse roomResponse=new MszsRoomResponse();
        BeanUtils.copyProperties(room,roomResponse);
        if (StringUtils.isNotEmpty(room.getFjImg())){
            List<String> imgs= Arrays.asList(room.getFjImg().split(","));
            roomResponse.setFjImg(imgs);
        }
        //酒店信息
        LambdaQueryWrapper<MszsHotel> hotelWrapper = new LambdaQueryWrapper<>();
        hotelWrapper.eq(MszsHotel::getId,room.getJdId());
        MszsHotel hotel = hotelService.getOne(hotelWrapper);
        if (Objects.nonNull(hotel)){
            roomResponse.setJdName(hotel.getJdName());
        }
        //房型信息
        LambdaQueryWrapper<MszsRoomType> typeWrapper = new LambdaQueryWrapper<>();
        typeWrapper.eq(MszsRoomType::getId,room.getFxId());
        MszsRoomType roomType = roomTypeService.getOne(typeWrapper);
        if (Objects.nonNull(roomType)){
            roomResponse.setFxName(roomType.getFxName());
        }

        return roomResponse;
    }

    @Override
    public List<MszsRoom> pageList(MszsRoomSelectRequest request) {

        PageHelper.startPage(request.getPage(), request.getLimit());
        LambdaQueryWrapper<MszsRoom> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(MszsRoom::getJdId,request.getJdId());
        if (StringUtils.isNotEmpty(request.getFjName())){
            lambdaQueryWrapper.like(MszsRoom::getFjName,request.getFjName());
        }
        lambdaQueryWrapper.orderByDesc(MszsRoom::getCreateTime);
        List<MszsRoom> hotelList = roomDao.selectList(lambdaQueryWrapper);
        if (CollUtil.isEmpty(hotelList)) {
            return CollUtil.newArrayList();
        }
        return hotelList;
    }

    @Override
    public List<MszsRoomResponse> roomPageList(MszsRoomSelectRequest request) {
        PageHelper.startPage(request.getPage(), request.getLimit());
        List<MszsRoomResponse>  page =  roomDao.hotelRoomList(request);
        for (MszsRoomResponse response:page){
            LambdaQueryWrapper<MszsHotel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(MszsHotel::getId,response.getJdId());
            MszsHotel hotel = hotelService.getOne(lambdaQueryWrapper);
            if (Objects.nonNull(hotel)){
                response.setJdName(hotel.getJdName());
                response.setJdPhone(hotel.getJdPhone());
            }
        }
        return page;
    }

    @Override
    public Integer rooms(MszsCenterRequest request) {
        LambdaQueryWrapper<MszsRoom> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        Integer uid = tokenComponent.getUserId();
        Integer count = 0;
        //根据登录信息 获取 酒店信息
        List<MszsHotelResponse> hotelList = hotelService.getUserHotelList(uid);
        if (request.getStatisticsType()==1){ //统计近七天
            for (MszsHotelResponse hotel:hotelList){
                Date dayDate = DateUtil.nowDateTimeReturnDate("yyyy-MM-dd"); //当前日期
                String daystr = DateUtil.addDay(dayDate,1,"yyyy-MM-dd"); //当前日期+1
                Date daystrDate = DateUtil.strToDate(daystr,"yyyy-MM-dd"); //日期字符串格式转换

                String serSeven = DateUtil.addDay(dayDate,-7,"yyyy-MM-dd"); //当前日期 -7
                Date sevenDate = DateUtil.strToDate(serSeven,"yyyy-MM-dd"); //日期字符串格式转换

                lambdaQueryWrapper.le(MszsRoom::getCreateTime,daystrDate);
                lambdaQueryWrapper.ge(MszsRoom::getCreateTime,sevenDate);
                lambdaQueryWrapper.eq(MszsRoom::getJdId,hotel.getId());
                lambdaQueryWrapper.eq(MszsRoom::getStatus,1);
                Integer cou = roomDao.selectCount(lambdaQueryWrapper);
                count = count+ cou;
            }
        }

        return count;
    }

    @Override
    public List<MszsRoom> getWhereStatusList(Integer status) {
        LambdaQueryWrapper<MszsRoom> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MszsRoom::getStatus,status);
        lambdaQueryWrapper.eq(MszsRoom::getIsDel,false);
        lambdaQueryWrapper.eq(MszsRoom::getIsShow,true);
        return roomDao.selectList(lambdaQueryWrapper);
    }

    @Override
    public MszsRoom getWhereStatusOne(String jdId,Integer status) {
        LambdaQueryWrapper<MszsRoom> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MszsRoom::getJdId,jdId);
        lambdaQueryWrapper.eq(MszsRoom::getStatus,status);
        return getOne(lambdaQueryWrapper,false);
    }

    @Override
    public List<MszsRoomResponse> notCheckInList(MszsRoomSelectRequest request) {
        PageHelper.startPage(request.getPage(), request.getLimit());
        List<MszsRoomResponse> list = roomDao.notCheckInList(request);
        return list;
    }

    @Override
    public List<MszsRoomResponse> checkInList(MszsRoomSelectRequest request) {
        PageHelper.startPage(request.getPage(), request.getLimit());
        List<MszsRoomResponse> list = roomDao.checkInList(request);
        for (MszsRoomResponse room:list){
            //获取房间入住记录
            request.setFjId(room.getId());
            List<MszsRoomOrderResponse> roomOrders = orderService.roomOrders(request);
            room.setRoomOrder(roomOrders);
        }
        return list;
    }

    //根据房间号获取信息
    MszsRoom roomNumber(String numbe){
        LambdaQueryWrapper<MszsRoom> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MszsRoom::getFjNumber,numbe);
        MszsRoom rooms = roomDao.selectOne(lambdaQueryWrapper);
        return rooms;
    }

}
